package demo1;

/**
 * Version: 1.1
 * Author: TangJun
 * Date: 2024-07-19
 */
class Student {
    public int age;

    public Student(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "age=" + age +
                '}';
    }
}
public class Test {
    public static void main(String[] args) {
        //构造
        String str1 = "abc";
        String str2 = new String("abc");
        char[] chs = {'A','b','C'};
        String str3 = new String(chs);

       //比较
        System.out.println(str1 == str2);
        System.out.println(str1.equals(str2));
        System.out.println(str1.equalsIgnoreCase(str3));
        System.out.println(str1.compareTo(str2));
        System.out.println(str1.compareToIgnoreCase(str3));

        //查找
        String test = "iiabcdefabcio";
        System.out.println(test.charAt(5));
        System.out.println(test.indexOf('b'));
        System.out.println(test.indexOf('b', 5));
        System.out.println(test.indexOf("abc"));
        System.out.println(test.indexOf("abc", 5));


        //转换
        String str1 = String.valueOf(123);
        String str2 = String.valueOf(123.12f);
        String str3 = String.valueOf(123.13);
        String str4 = String.valueOf(true);
        String str5 = String.valueOf(new Student(18));
        System.out.println(str1);
        System.out.println(str2);
        System.out.println(str3);
        System.out.println(str4);
        System.out.println(str5);
        int a = Integer.parseInt(str1);
        System.out.println(a);

        String up = "QWER";
        String low = "asdfg";
        String upToLow = up.toLowerCase();
        String lowToUp = low.toUpperCase();
        System.out.println(upToLow);
        System.out.println(lowToUp);

        String str = "abcde";
        char[] chars = str.toCharArray();
        for (char x : chars) {
            System.out.println(x);
        }

        String test = String.format("%d-%d-%d", 2024, 7, 19);
        System.out.println(test);


        //替换
        String str = "abcasjabcowa";
        String ret1 = str.replace('a', '*');
        String ret2 = str.replace("ab", "KKK");
        String ret3 = str.replaceAll("abc", "******");
        String ret4 = str.replaceFirst("abc", "QQQQQ");
        System.out.println(ret1);
        System.out.println(ret2);
        System.out.println(ret3);
        System.out.println(ret4);
        //拆分
        String str = "127.0.0.1";
        String[] ret1 = str.split("\\.");
        String[] ret2 = str.split("\\.", 3);
        for (String x : ret1) {
            System.out.println(x);
        }
        for (String x : ret2) {
            System.out.println(x);
        }

        //截取
        String str = "abcdefgh";
        String ret1 = str.substring(2);
        String ret2 = str.substring(2, 4);
        System.out.println(ret1);
        System.out.println(ret2);
        //其他方法
        String str = "   hello        ";
        String test = "pppppppppppp";
        String ret = str.trim();
        System.out.println(str + test);
        System.out.println(ret + test);

        String str1 = "hello";
        System.out.println(str1.contains("ll"));
        System.out.println(str1.endsWith("llo"));
        System.out.println(str1.startsWith("hel"));
    }
}
